
class Car():
    odometer_reading = 0
    def __init__(self,make,model,year):
        self.make = make
        self.model = model
        self.year = year

    def get_descriptive(self):
        "返回整洁的描述信息"
        long_name = str(self.year)+" "+self.make+" "+self.model
        return long_name.title()

    def read_odometer(self):
        "打印一条指出汽车里程的信息"
        print("This car has "+ str(self.odometer_reading)+" miles on it.")

    def update_odometer(self,mileage):
        "通过此方法修改属性的值"
        "禁止将里程表读数往回调"
        if mileage < self.odometer_reading:
            print("You can't roll back an odometer")
        else:
            self.odometer_reading = mileage

    def increment_odometer(self, miles):
        "里程增加方法"
        self.odometer_reading += miles

    def fill_gas_tank(self):
        print("This car has 70L gas")


class ElectricCar(Car):
    #特有属性
    #battery_size = 100


    "电动汽车继承普通汽车"
    def __init__(self,make,model,year):
        "初始化父类的属性"
        "创建子类的实例时，Python首先要完成的任务是给父类的所有属性赋值"
        super(ElectricCar, self).__init__(make,model,year)
        self.battery = Battery(100)##将实例作为属性

    def describe_battery(self):
        "打印一条描述电瓶容量的消息"
        print("This car has a "+str(self.battery.battery_size)+"-KWH battery.")

    def fill_gas_tank(self):
        "重写父类方法"
        "电动汽车没有油箱"
        print("This car doesn't need a gas tank")


class Battery():
    "一次模拟电动汽车电瓶的简单尝试"
    def __init__(self,battery_size = 70):
        "初始化电瓶的属性"
        self.battery_size = battery_size

    def describe_battery(self):
        "打印一条描述电瓶容量的消息"
        print("This car has a " + str(self.battery_size) + "-KWH battery.")

    def get_range(self):
        "打印一条消息，指出电瓶的续航里程"
        if self.battery_size == 70:
            range = 240
        elif self.battery_size == 85:
            range = 270
        elif self.battery_size == 100:
            range = 300

        message = "This car can go approximately "+str(range)
        message += "miles on a full charge."
        print(message)






if __name__ == '__main__':
    audi = Car('audi','a7',2020)
    print(audi.get_descriptive())
    audi.read_odometer()
    audi.update_odometer(77)
    audi.read_odometer()
    audi.update_odometer(60)
    audi.increment_odometer(100)
    audi.read_odometer()

    tesla = ElectricCar("tesla","model S","2020")
    print(tesla.get_descriptive())
    tesla.fill_gas_tank()#重写
    tesla.describe_battery()
    tesla.battery.get_range()

